<?php
/**
 * 周舟 hzboye010@163.com
 * addby sublime snippets
 */
namespace Webview\Controller;

use Webview\Controller\WebViewController;
use Common\Api\ScoreGoodsApi;
use Admin\Api\MemberApi;
use Common\Api\ExchangeLogApi;
use Admin\Api\AddressApi;
use Tool\Api\AreaApi;
use Tool\Api\CityApi;
use Tool\Api\ProvinceApi;

class ScoreGoodsController extends WebviewController{
    /**
     * 初始化
     */
    public function _initialize(){
        parent::_initialize();
        // $this->assign('isapp','com.itboye.sunsun://TYPE=SGOODS_DETAIL');
        //详情页
        // $this->assign('isapp1','com.itboye.sunsun://TYPE=MORE_SCORE');
        //
        // $this->assign('isapp2','com.itboye.sunsun://TYPE=SHOW_LOG');
        //
        // $this->assign('isapp','');
    }
    /**
     * 积分商城页面入口
     */
    public function index(){
      $uid = $this->uid;
      // dump($uid);exit;
      
      $r = apiCall(MemberApi::GET_INFO,array(array('uid'=>$uid)));
      // dump($r);exit;
      $score = $r['info']['score'];
      $this->assign('score',$score);
      $this->assignTitle('积分商城');
      $this->display('ScoreShop:index');
    }
    /**
     * ajax获取积分商品
     * @param  int      the start position
     * @param  int      pagesize
     * @return [json]
    */
    public function getSGoods($start,$size){
        $r = apiCall(ScoreGoodsApi::QUERY,array($start,$size,''));
        if($r['status']){
            foreach ($r['info'] as $k => $v) {
              $r['info'][$k]['url'] = U('ScoreGoods/detail',array('uid'=>$this->uid,'psw'=>$this->psw,'pid'=>$v['id']));
            }
        }
        echo json_encode($r);
    }

    // public function test(){
    //     // echo json_encode(apiCall(GameApi::TEST,array($uid,$gid)));
    //     echo '---';
    // }

    /**
     * 商品详情查看
     */
    public function detail() {
        if (IS_GET) {
            $token  = md5(time());
            session('paycheck',$token);
            $id    = I('get.id', 0);

            $result = apiCall(ScoreGoodsApi::GET_INFO,array(array('id'=>$id)));
            // dump($result['info']);exit;
            $this -> assign ('entry',$result['info']);
            $this -> assignTitle($result['info']['name']);
            $this -> assign ('paycheck',$token);
            $this -> display('ScoreShop:detail');
        }
    }
    public function getArea($cid=1017,$type=1){
        $cid  = intval($cid);
        $type = intval($type);
        if($type === 2)
          $r = apiCall(AreaApi::GET_LIST_BY_CITY_ID,array($cid));
        elseif($type === 1)
          $r = apiCall(CityApi::GET_LIST_BY_PROVINCE_ID,array($cid));
        // elseif($type === 1)
        //   $r = apiCall(ProvinceApi::QUERY_NO_PAGING,array());
       echo json_encode($r);

    }
    public function addressset($id){
      $aid = I('addressid');
      $r = apiCall(AddressApi::GET_INFO,array(array('id'=>$aid)));
      // dump($r);exit;
      if(!$r['status']){
        echo json_encode($this->error($r['info']));exit;
      }else{
        $map = array();
        $r2 = apiCall(ExchangeLogApi::GET_INFO,array(array('id'=>$id)));
        if($r2['info']['price'] == 0) $map['exchange_status'] = 0;
        unset($r2);
        $map['address_id'] = $aid;
        $map['address_province'] = $r['info']['province'];
        $map['address_city'] = $r['info']['city'];
        $map['address_area'] = $r['info']['area'];
        // dump($map);exit;
        $r = apiCall(ExchangeLogApi::SAVEBYID,array($id,$map));
        echo json_encode($r);
      }
    }
    public function addressadd(){
        // $get = I('get.');
        // dump($get);
        $map = I('post.');
        // dump($map);exit;
        // if(!$map['info']['catid']) exit('非法地区信息');
        if(!$map['provinceid'])   exit('错误省份');
        if(!$map['cityid'])   exit('错误城市');
        if(!$map['areaid'])   exit('错误地区');
        if(!$map['detailinfo'])   exit('错误地址');
        if(!$map['postal_code'] || !preg_match('/^\d{6}$/',$map['postal_code']))   exit('错误邮编');
        if(!$map['mobile'] || !preg_match('/^1[34578]\d{9}$/',$map['mobile']))        exit('错误手机号');
        if(!$map['contactname'])   exit('错误联系人');
        $map['uid'] = $this->uid;
        $map['country'] = '中国';
        $map['id_card'] = '';
        $r = apiCall(AddressApi::ADD,array($map));
        if(!$r['status']){
            echo json_encode(array('status'=>false,'info'=>'address add error'));
        }else{
            $id = intval($r['info']);
            // echo 'id'.$id;
            $r = apiCall(AddressApi::GET_INFO,array(array('id'=>$id)));
            echo json_encode($r);
        }
        exit;
    }
    public function exchange($pid){
      echo json_encode(apiCall(ScoreGoodsApi::EXCHANGE,array($this->uid,$this->psw,$pid)));
    }
}